package com.tianma.sync;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.tianma.model.BinlogValue;
import com.tianma.model.InnerBinlogEntry;

import java.util.List;
import java.util.Map;

public abstract class AbstractSyncTemplate {
    public void sync(InnerBinlogEntry innerBinlogEntry) throws Exception {
        CanalEntry.EventType eventType = innerBinlogEntry.getEventType();

        List<Map<String, BinlogValue>> rows = innerBinlogEntry.getRows();
        if(rows == null) return;

        switch (eventType) {
            case INSERT:
                insert(rows);
                break;
            case UPDATE:
                update(rows);
                break;
            case DELETE:
                delete(rows);
                break;
        }

    }

    /**
     * 新增记录
     * @param rows
     */
    abstract void insert(List<Map<String, BinlogValue>> rows) throws Exception;

    /**
     * 更新记录
     * @param rows
     */
    abstract void update(List<Map<String, BinlogValue>> rows) throws Exception;

    /**
     * 删除记录
     * @param rows
     */
    abstract void delete(List<Map<String, BinlogValue>> rows);
}
